let express = require('express');
let expressJWT = require('express-jwt');
let cors = require('cors');

let app = express();
app.listen(3000, () => console.log('3000'));

//-----   中间件   -----
app.use(cors());
app.use(express.urlencoded({
    extended: false
}));
app.use(expressJWT({
    secret: 'abc',
    algorithms: ['HS256']
}).unless({
    path: ['/user/login', '/user/reg']
}));


//----  设计路由  ----
// 登录
let login = require("./routers/login.js")
app.use("/user", login);

// todo
let todo = require("./routers/todo.js")
app.use("/todo", todo);


//----  处理错误中间件  ----
app.use(function (err, req, res, next) {
    if (err.name == "UnauthorizedError") {
        res.send({
            status: 1,
            message: "身份认证失败！"
        });
    }
});